import * as THREE from "three";
import ContextMenu, { type MenuItem } from "@imengyu/vue3-context-menu";
import { BaseMode } from "./BaseMode";
import { SingleAssemblyProductMode } from "./SingleAssemblyProductMode";
import {
  Part3DMesh,
  Loft3DMesh,
  MouseSenseCube,
} from "../types/DrawingGroups/meshs";
import {
  AssemblyProductGroup,
  Product3DGroup,
  Sub3DGroup,
  Base3DGroup
} from "../types/DrawingGroups/groups";
import { changeMode } from "..";
import type { KujialeParamModel } from "../types/KujialeJsonModels/KujialeParamModel";
import { SingleProductMode } from "./SingleProductMode";
import type { KujialeAssemblyModel } from "../types/KujialeJsonModels/KujialeAssemblyModel";
import type { AssemblyProductModel, BaseModel } from "../types/Models";
import { useCurrentModelStore } from "@/stores/current";

export class InitMode extends BaseMode {
  constructor(
    dom: HTMLCanvasElement,
    camera: THREE.Camera,
    scene: THREE.Scene
  ) {
    super(dom, camera, scene);
    this.modeName = "init";

    //this.init();
  }

  init() {
    this.mouseSenseCubes = this.findMouseSenseCubes();

    var store = useCurrentModelStore();
    store.setCurrentModel(
      this.scene.children.filter((g) => g instanceof Base3DGroup)
    );
  }

  mousemove(e: MouseEvent) {
    this.funcRaycaster(e);
  }

  mousedown(e: MouseEvent) { 
    var store = useCurrentModelStore();
    
    if (this.currentProductCube && this.currentProductCube.visible) {
      if (this.currentProductCube.parent instanceof Product3DGroup) {
        store.setCurrentModel([this.currentProductCube.parent as Product3DGroup]);
        store.setCurrentRightTabName("model");
        var [json] = this.currentProductCube.parent.model.kujialeModel!;
        this.showProductContextMenu(
          e,
          json,
          this.currentProductCube.parent.model
        );
      } else if (
        this.currentProductCube.parent instanceof AssemblyProductGroup
      ) {
        store.setCurrentModel([this.currentProductCube.parent as AssemblyProductGroup]);
        store.setCurrentRightTabName("model");
        var model = this.currentProductCube.parent
          .model as AssemblyProductModel;
        this.showAssemblyProductContextMenu(
          e,
          model.kujialeAssemblyModel,
          this.currentProductCube.parent.model
        );
      }
    }
  }

  findMouseSenseCubes(): THREE.Mesh[] {
    const meshes: THREE.Mesh[] = [];
    this.scene.children.forEach((g) => {
      if (g instanceof Product3DGroup || g instanceof AssemblyProductGroup) {
        const cube = g.children[0];
        if (cube instanceof MouseSenseCube) {
          meshes.push(cube as THREE.Mesh);
        }
      }
    });
    return meshes;
  }
  showProductContextMenu(
    e: MouseEvent,
    model: KujialeParamModel,
    basemodel: BaseModel
  ) {
    ContextMenu.showContextMenu({
      x: e.x,
      y: e.y,
      items: [
        {
          label: model.modelName,
          children: [
            {
              label: `尺寸: ${model.boxSize?.x}/${model.boxSize?.z}/${model.boxSize?.y}`,
            },
            {
              label: `坐标: ${model.position.x}/${model.position.y}/${model.position.z}`,
            },
            {
              label: `旋转: ${model.rotateDegree.x}/${model.rotateDegree.y}/${model.rotateDegree.z}`,
            },
          ],
        },
        {
          label: "材质",
          children: this.getCurrentMenuTextureList(),
        },
        {
          label: basemodel.doorClosed ? "开门" : "关门",
          onClick: this.findDoorsAndAction.bind(this, basemodel.doorClosed, () => {
            basemodel.doorClosed = !basemodel.doorClosed;
          }),
        },
        // {
        //   label: "删除",
        //   onClick: () => {},
        // },
        {
          label: "检视",
          onClick: () => {
            changeMode(
              new SingleProductMode(
                this.dom,
                this.camera,
                this.scene,
                this.currentProductCube!.parent as Product3DGroup,
                this
              ),
              false
            );
          },
        },
      ],
    });
  }
  showAssemblyProductContextMenu(
    e: MouseEvent,
    model: KujialeAssemblyModel,
    basemodel: BaseModel
  ) {
    ContextMenu.showContextMenu({
      x: e.x,
      y: e.y,
      items: [
        {
          label: model.name,
          children: [
            {
              label: `尺寸: ${model.size.x}/${model.size.z}/${model.size.y}`,
            },
            {
              label: `坐标: ${model.position.x}/${model.position.y}/${model.position.z}`,
            },
            {
              label: `旋转: ${model.rotateDegree.x}/${model.rotateDegree.y}/${model.rotateDegree.z}`,
            },
          ],
        },
        {
          label: "材质",
          children: this.getCurrentMenuTextureList(),
        },
        // {
        //   label: "删除",
        //   onClick: () => {},
        // },
        {
          label: basemodel.doorClosed ? "开门" : "关门",
          onClick: this.findDoorsAndAction.bind(this, basemodel.doorClosed, () => {
            basemodel.doorClosed = !basemodel.doorClosed;
          }),
        },
        {
          label: "检视",
          onClick: () => {
            changeMode(
              new SingleAssemblyProductMode(
                this.dom,
                this.camera,
                this.scene,
                model
              ),
              false
            );
          },
        },
      ],
    });
  }
}
